<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
if(isset($_G['usergroup']['access']["nav$f"]) && !ican("nav$f")){
	msg('没有权限');
}
$m = new NavModule;
$m->$f();
class NavModule
{
	function index() {//列表
		global $_G;
		$wheresql = '';
		$page = max(1, getGP('page','G','int'));
		$pagesize = 50;
		$offset = ($page - 1) * $pagesize;
		$url = 'admin.php?m=nav';
		$navtypes=array('1'=>'主导航','二级导航','顶部导航','底部导航','友情链接1','友情链接2','友情链接3');
	
		if ($keyword = getGP('keyword','G')) {
			$wheresql .= " AND (l.name like '%$keyword%' OR l.url LIKE '%$keyword%' OR l.title LIKE '%$keyword%')";
			$url .= '&keyword='.rawurlencode($keyword);
		}
		if (getGP('visible','G') == 'yes') {
			$wheresql .= " AND l.visible = 1";
			$url .= '&visible=yes';		
		} elseif (getGP('visible','G') == 'no') {
			$wheresql .= " AND l.visible = 0";
			$url .= '&visible=no';		
		}
		if ($navtype = getGP('navtype','G','int')) {
			$wheresql .= " AND l.navtype = $navtype";
			$url .= '&navtype='.$navtype;	
		}
	
		$num = $_G['db']->result("SELECT COUNT(*) AS num FROM".table('nav')."l WHERE 1 $wheresql");
	
		$query= $_G['db']->query("SELECT l.* FROM".table('nav')."l  WHERE 1 $wheresql ORDER BY l.navtype ASC, l.displayorder ASC LIMIT $offset, $pagesize");
		while($data=$_G['db']->fetch_array($query)) {
			if( $data['parentid'] ){
				$subnav[$data['parentid']][] = $data;
			} else {
				$result[]=$data;
			}
		}
		include admintemplate('nav');
	}
	function add(){
		global $_G;
		$navtypes=array('1'=>'主导航','二级导航','顶部导航','底部导航','友情链接1','友情链接2','友情链接3');
		$navtype = getGP('navtype','G','int');
		if( !$navtype ) $navtype = 1;
		$parents = $_G['db']->fetch_all( "SELECT * FROM".table('nav')."WHERE navtype='".$navtype."' AND parentid=0  ORDER BY navtype ASC, displayorder ASC");
		include admintemplate('nav_add');
	}
	function save(){
		if(!getGP('savetype','P')){
			msg('未知错误', 'admin.php?m=nav');
		}
		if(!ican("navedit") && !ican("navadd")){
			msg('没有权限');
		}
		global $_G;
		$savetype = getGP('savetype','P');
		$id = getGP('id','P','int');
		$link = array(
			'name' => check_str(getGP('name','P')),
			'navtype' => getGP('navtype','P','int'),
			'parentid' => getGP('parentid','P','int'),
			'title' => check_str(getGP('title','P')),
			'url' => getGP('url','P'),
			'target' => getGP('target','P','int'),
			'access' => getGP('access','P','int'),
			'displayorder' => getGP('displayorder','P','int'),
			'visible' => getGP('visible','P','int')
		);
		if ($savetype == 'add') {
			if ($_G['db']->insert('nav',$link)) {
				recache('nav');
				msg('成功添加一个导航', 'admin.php?m=nav');
			}
		} elseif ($savetype) {
			$_G['db']->update('nav',$link, array('id'=>$id));
			recache('nav');
			msg('修改成功', 'admin.php?m=nav');
		}
	}
	function edit(){
		global $_G;
		$id = getGP('id','G','int');
		if ($result = $_G['db']->fetch_one_array("SELECT * FROM".table('nav')."WHERE id = '$id'")) {
			$navtypes=array('1'=>'主导航','二级导航','顶部导航','底部导航','友情链接1','友情链接2','友情链接3');
			$parents = $_G['db']->fetch_all( "SELECT * FROM".table('nav')."WHERE navtype='".$result['navtype']."' AND parentid=0 AND id!={$id} ORDER BY navtype ASC, displayorder ASC");
			$navtype=$result['navtype'];
			include admintemplate('nav_edit');
		} else {
			msg('导航不存在');
		}
	}
	function delete(){
		global $_G;
		$idarr = getGP('id','P','array');
		if(count($idarr)>0) {
			$_G['db']->query("DELETE FROM".table('nav')." WHERE id IN(".implode(',', $idarr).")");
		}
		recache('nav');
		msg('成功删除选中的链接',$_G['referurl']);
	}
	function move(){
		global $_G;
		if(!ican("navedit") && !ican("navadd")){
			msg('没有权限');
		}
		$navtype = getGP('navtype','P','int');
		$idarr = getGP('id','P','array');
		if(count($idarr)>0) {
			$_G['db']->query("UPDATE".table('nav')."SET navtype = '$navtype' WHERE id IN(".implode(',', $idarr).")");
		}
		recache('nav');
		msg('成功移动选中的链接',$_G['referurl']);
	}
	function resort(){
		global $_G;
		if(!ican("navedit") && !ican("navadd")){
			msg('没有权限');
		}
		$idarr = getGP('displayorder','P','array');
		foreach ($idarr as $id => $orderid) {
			$_G['db']->query("UPDATE".table('nav')."SET displayorder = '$orderid' WHERE id = '$id'");
		}
		recache('nav');
		msg('成功更新排序',$_G['referurl']);
	}
	function getparent(){
		global $_G;
		$data['s']=false;
		if(!ican("navedit") && !ican("navadd")){
			echo json_encode($data);exit;
		}
		$navtype = getGP('navtype','G','int');
		if( !$navtype ) $navtype = 1;
		$data['d'] = $_G['db']->fetch_all( "SELECT id,name FROM".table('nav')."WHERE navtype='".$navtype."' AND parentid=0 ORDER BY navtype ASC, displayorder ASC");
		if(!empty($data['d']))$data['s']=true;
		echo json_encode($data);exit;
	}
	
}
?>